<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input placeholder="Title" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getList">
        Search
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        Add
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="No" sortable="custom" align="center" width="60" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="页面名称" sortable="custom" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{ row.pageName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="页面操作(点击详情授权角色)" sortable="custom" align="center" width="400">
        <template slot-scope="{row}">
          <el-button v-for="authority in row.authority" :key="authority.id" v-text="authority.authorityName" />
        </template>
      </el-table-column>
      <el-table-column label="操作" sortable="custom" align="center" width="400">
        <template slot-scope="{row}">
          <el-button @click=" handleShowDetail(row)">详情</el-button>
          <el-button @click="handleModify(row.$index, row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <!--  add  -->
    <el-dialog title="添加" :visible.sync="createDialogVisible">
      <el-form ref="dataForm" :rules="addRules" :model="addResource" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="资源名称" prop="name">
          <el-input v-model="addResource.name" name="name" />
        </el-form-item>
        <el-form-item label="页面操作">
          <el-input v-model="addResource.option" name="name" />
        </el-form-item>
        <el-button @click="handleInsertOption">
          添加操作
        </el-button>
        <el-table
          :data="addResource.options"
          border
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="操作名称" align="center" width="200">
            <template slot-scope="{row}">
              <span>{{ row.option }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="{row}">
              <el-button @click.native.prevent="deleteRow(row.$index, addResource.options)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="createDialogVisible = false">
          Cancel
        </el-button>
        <el-button @click="handleCreateCommit">
          Ok
        </el-button>
      </div>
    </el-dialog>
    <!--  modify  -->
    <el-dialog title="修改" :visible.sync="ModifyDialogVisible">
      <el-form ref="dataForm" :rules="addRules" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="资源名称" prop="name">
          <el-input v-model="modifyRow.pageName" name="name" />
        </el-form-item>
        <el-form-item label="页面操作">
          <el-input v-model="addResource.option" name="name" />
        </el-form-item>
        <el-button>
          添加操作
        </el-button>
        <el-table
          :data="modifyRow.authority"
          border
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="操作名称" align="center" width="200">
            <template slot-scope="{row}">
              <span>{{ row.authorityName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="{row}">
              <el-button @click.native.prevent="deleteRow(row.$index, addResource.options)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="createDialogVisible = false">
          Cancel
        </el-button>
        <el-button @click="handleCreateCommit">
          Ok
        </el-button>
      </div>
    </el-dialog>
    <!--  资源详情  -->
    <el-dialog title="资源详情" :visible.sync="showDetailDialogVisible">
      <h1>{{ detailRow.pageName }}</h1>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDetailDialogVisible = false">
          Ok
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import permission from '@/directive/permission' // 权限判断指令
import checkPermission from '@/utils/permission'
import '@/utils/validate'
import { fetchAllResource } from '@/api/resourcePage/resourcePageApi'
import waves from '@/directive/waves'

export default {
  components: { Pagination },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      createDialogVisible: false,
      addRules: {},
      addResource: { name: '', options: [], option: '' },
      dialogTitle: '增加',
      modifyIndex: -1,
      modifyRow: '',
      ModifyDialogVisible: false,
      showDetailDialogVisible: false,
      detailRow: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    },
    handleFilter() {
    },
    handleCreate() {
      this.createDialogVisible = true
    },
    getList() {
      this.listLoading = true
      console.log('getList')
      fetchAllResource(this.listQuery).then(response => {
        console.log(response.data)
        this.list = response.data.items
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleInsertOption() {
      for (const i in this.addResource.options) {
        if (this.addResource.options[i].option.trim() === this.addResource.option) {
          window.alert('不可重复添加')
          return
        }
      }
      this.addResource.options.push({ option: this.addResource.option })
      this.addResource.option = ''
      console.log(this.addResource.options)
    },
    handleDeleteOption(data) {

    },
    deleteRow(index, rows) {
      rows.splice(index, 1)
    },
    handleCreateCommit() {
      if (this.addResource.name === '') {
        window.alert('不可添加空信息')
        return
      } else {
        const arr = []
        for (const i in this.addResource.options) {
          arr.push({ id: i, tag: 'Delete', authorityName: this.addResource.options[i].option })
        }
        this.list.push({ pageName: this.addResource.name, authority: arr })
        this.addResource = { name: '', options: [], option: '' }
        this.createDialogVisible = false
      }
    },
    handleModify(index, data) {
      console.log('modify')
      this.modifyRow = data
      this.modifyIndex = index
      this.ModifyDialogVisible = true
    },
    handleShowDetail(row) {
      this.detailRow = row
      this.showDetailDialogVisible = true
    }
  }
}
</script>

<style scoped>

</style>
